<?php
require_once dirname(__FILE__) . "/../config/functions.php";
include_once dirname(__FILE__) . '/header.php';

if (isset($_POST) && $_POST) {
    foreach ($_POST as $key => $value) {
        //check is exist
        $check = mysql_query("SELECT * FROM `option` where `key` = '$key' ");
        if (mysql_num_rows($check) > 0) {
            mysql_query("update `option` set `value` = '$value' where `key` = '$key' ");
        } else {
            mysql_query("insert into `option` (`value`, `key`) values ('$value', '$key') ");
        }
    }
}

//get all options
$options = mysql_query("SELECT * FROM `option`");
$allOprionsArray = array();
while ($option = mysql_fetch_array($options)) {
    $allOprionsArray[$option['key']] = $option['value'];
}
?>
<div id="page-wrapper">
    <div class="row">
        <div class="col-lg-12">
            <h1>Meta Settings <small>Update Options</small></h1>
            <?php if (isset($_POST) && $_POST) : ?>
                <div class="alert alert-success alert-dismissable">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                    Settings updated successfully.
                </div>
            <?php endif; ?>

            <form class="form-horizontal" role="form" method="post">
            <ol class="breadcrumb" style="padding-left: 25px;">
                <div class="form-group">
                    <label for="welcome_email_subject" class="col-sm-2 control-label">Site Title</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" onkeyup="writeOnOtherField()" value="<?= (isset($allOprionsArray['site_name']))?$allOprionsArray['site_name']:''; ?>" class="" name="site_name" id="siteName">
                    </div>
                </div>
                <input style="display: none;" type="text" name="site_name" />
                <input style="display: none;" type="text" name="site_name"  />
                
                <div class="form-group">
                    <label for="welcome_email_subject" class="col-sm-2 control-label">Site Description</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" name="site_description" placeholder="paste here your site description - no more then 180 symbols" onkeyup="writeOnOtherField()" value="<?= (isset($allOprionsArray['site_description']))?$allOprionsArray['site_description']:''; ?>"/>
                    </div>
                </div>
                <input style="display: none;" type="text" name="site_description"  />
                
                <div class="form-group">
                    <label for="welcome_email_subject" class="col-sm-2 control-label">Site Keywords</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" name="keywords" placeholder="paste here your keywords, separate them by ","" value="<?= (isset($allOprionsArray['keywords']))?$allOprionsArray['keywords']:''; ?>"/>
                    </div>
                </div>
                
                
                <div class="form-group">
                    <label for="welcome_email_subject" class="col-sm-2 control-label">Valid Site Url</label>
                    <div class="col-sm-10">
                        <input class="form-control"  type="text" name="url" value="<?= (isset($allOprionsArray['url']))?$allOprionsArray['url']:''; ?>"/>
                    </div>
                </div>
                
                
                <div class="form-group">
                    <label for="welcome_email_subject" class="col-sm-2 control-label">Site Image</label>
                    <div class="col-sm-10">
                        <input class="form-control" type="text" name="image" placeholder="paste here url to your image - min 800X600 pix" value="<?= (isset($allOprionsArray['image']))?$allOprionsArray['image']:''; ?>"/>
                    </div>
                </div>
                
                
               <div class="form-group">
                    <label for="author" class="col-sm-2 control-label">Your Name</label>
                    <div class="col-sm-10">
                        <input class="form-control" type="text" name="author" value="<?= (isset($allOprionsArray['author']))?$allOprionsArray['author']:''; ?>"/>
                    </div>
                </div>
                                
                <input type="text" style="display: none;" name="admins" value="<?= (isset($allOprionsArray['admins']))?$allOprionsArray['admins']:''; ?>"/>
                <input type="text" style="display: none;" name="Robots" value="<?= (isset($allOprionsArray['Robots']))?$allOprionsArray['Robots']:'INDEX,NOFOLLOW'; ?>"/>
                <input type="text" style="display: none;" name="type" value="<?= (isset($allOprionsArray['type']))?$allOprionsArray['type']:'website'; ?>"/>
                
            </ol>

                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10" style="margin-left: 0;">
                        <button type="submit" class="btn btn-primary">Update</button>
                    </div>
                </div>
            </form>

        </div>
    </div><!-- /.row -->

</div><!-- /#page-wrapper -->
<?php include_once dirname(__FILE__) . '/footer.php'; ?>

<script>

    function writeOnOtherField(){
         setTimeout(function(){

         $('input:enabled').each(function(){
            $("[name="+$(this).attr('name')+"]").val($(this).val());    
         });}
            ,100);

        
    }
    $(document).ready(function(){
        writeOnOtherField();
    });

</script>